//------------------------------------------------------------------------------
//  <copyright file="TriangleElement.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------

namespace Microsoft.Robotics.Geometry
{
    using System.Runtime.Serialization;

    using Microsoft.Robotics.Numerics;

    /// <summary>
    /// A class representing a triangle in the world.
    /// The vertices should be in a clockwise order if
    /// we see the triangle from its front side.
    /// The normal of the triangle is pointing to the
    /// front side of the triangle.
    /// </summary>
    [DataContract]
    public class TriangleElement : GeometryElement3D
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TriangleElement" /> class.
        /// </summary>
        /// <param name="v0">Vertex 0</param>
        /// <param name="v1">Vertex 1</param>
        /// <param name="v2">Vertex 2</param>
        public TriangleElement(Vector3 v0, Vector3 v1, Vector3 v2)
            : base()
        {
            this.V0 = v0;
            this.V1 = v1;
            this.V2 = v2;

            this.Normal = Vector3.Normalize(Vector3.Cross(v2 - v0, v1 - v0));
        }

        /// <summary>
        /// Gets vertex 0
        /// </summary>
        [DataMember]
        public Vector3 V0 { get; private set; }

        /// <summary>
        /// Gets vertex 1
        /// </summary>
        [DataMember]
        public Vector3 V1 { get; private set; }

        /// <summary>
        /// Gets vertex 2
        /// </summary>
        [DataMember]
        public Vector3 V2 { get; private set; }

        /// <summary>
        /// Gets the normal direction.
        /// This normal points to the front side of the triangle.
        /// It is computed as cross(V0V2, V0V1)
        /// </summary>
        [DataMember]
        public Vector3 Normal { get; private set; }

        /// <summary>
        /// Compute the distances from another world element
        /// </summary>
        /// <param name="e">A world element</param>
        /// <returns>The distances from a world element</returns>
        public override double DistanceFrom(GeometryElement3D e)
        {
            if (e is SphereElement)
            {
                return GeometricCalculations.ComputeDistance(this, (SphereElement)e);
            }
            else
            {
                // Although there are algorithms on computing triangle-triangle intersection
                // we leave this unimplemented at this time.  Reason is:
                // We will use spheres to represent the robot arm and robot hand.
                // For the environment, we use both spheres and triangles to represent
                // its geometry.  In this assumption, there should not be a need to compute
                // the distances between two triangles.
                throw new System.NotImplementedException("Triangle vs. triangle distance computation is not yet implemented.  Please try to avoid the situation that you need it.  General suggestion: Use triangles or spheres to represent the world; Use only spheres to represent the robot and any attachment on the robot end effector");
            }
        }
    }
}
